/*******************************************************************************
********************************************************************************
	      On-Demand Assistance: Experimental Evidence from Indonesia  
********************************************************************************						
********************************************************************************

  Authors:              Rema Hanna, Harvard Kennedy School
                        Benjamin A. Olken, MIT
                        Sudarno Sumarto, SMERU
                        Achmad Maulana, Universitas Padjadjaran
                        Vivi Alatas, Asakreativita
                        Elan Satriawan, Gadjah Mada University 
  
  Description:          Runs all do-files for the Kartu Prakerja Paper.
     
********************************************************************************
*******************************************************************************/


clear all
set more off
macro drop _all
version 18.0

********************************************************************************
**#                     1. Set Filepaths: Base Folders
********************************************************************************

if "`c(username)'" ==  "FILL IN" {   // Fill in username
  global KP_base       "FILL IN"     // Fill in code & output folder
  global KP_base_data  "FILL IN"     // Fill in data folder
  }

else {
  di as err "User is not recognized."
  exit
}

********************************************************************************
**#                     2. Set Filepaths:  Subfolders
********************************************************************************

* Data Folders
global KP_data              "$KP_base_data/Data"
global KP_deid_admin        "$KP_data/Admin"
global KP_deid_survey       "$KP_data/Survey"
global KP_deid_sakernas     "$KP_data/SAKERNAS"
global KP_deid_susenas      "$KP_data/SUSENAS"
global KP_deid_misc         "$KP_data/Misc"

* Code/Output/Log/Ado folders
global KP_output            "$KP_base/Output"
global KP_do                "$KP_base/Do"
global KP_logs              "$KP_base/Logs"
global KP_ado               "$KP_base/Ado"

********************************************************************************
**#                     3. Set Switches
********************************************************************************

* 1) Run Master
global master_run           = 1

* 2) Ado files
global ado_files            = 1

* 3) Install packages
global install_packages     = 0

* 4) Merge PMO data
global merge_PMO            = 1

* 5) Clean PMO data
global clean_PMO            = 1

* 6) Clean SAKERNAS data
global clean_SAKERNAS       = 1

* 7) Clean SUSENAS data
global clean_SUSENAS        = 1

* 8) Survey data cleaning
global clean_survey21       = 1

* 9) Survey data cleaning
global clean_survey22       = 1

* 10) Combined cleaning
global clean_combined       = 1

* 11) Tables and Figures
global tables_figures       = 1

* 12) Suffix date
local day = string(day(dofc(clock("`c(current_date)'", "DMY"))), "%02.0f")
local month_num = month(dofc(clock("`c(current_date)'", "DMY")))
local month : word `month_num' of jan feb mar apr may jun jul aug sep oct nov dec
global suffix = "`month'_`day'"


********************************************************************************
**#                     4. Make Data Directories
********************************************************************************
*---- Admin data ----*
cap mkdir "$KP_deid_admin/Clean"

*---- Online Surveys ----*
cap mkdir "$KP_deid_survey/2021/Clean"
cap mkdir "$KP_deid_survey/2022/Clean"

*---- Sakernas ----*  
cap mkdir "$KP_deid_sakernas/Clean"

*---- Susenas ----*
cap mkdir "$KP_deid_susenas/Clean"

*---- Misc ----*
cap mkdir "$KP_deid_misc/Clean"

********************************************************************************
**#                     5. Make Output Directories
********************************************************************************

cap mkdir $KP_output
cap mkdir $KP_output/tables
cap mkdir $KP_output/figures
cap mkdir $KP_output/tables/descriptive
cap mkdir $KP_output/tables/descriptive/sterfiles
cap mkdir $KP_output/tables/descriptive/tex
cap mkdir $KP_output/tables/impacts
cap mkdir $KP_output/tables/impacts/sterfiles
cap mkdir $KP_output/tables/impacts/tex
cap mkdir $KP_output/tables/impacts/csv
cap mkdir $KP_output/figures/descriptive
cap mkdir $KP_output/figures/descriptive/pdf
cap mkdir $KP_output/figures/descriptive/png
cap mkdir $KP_logs

********************************************************************************
**#                     6. Install Required Packages
********************************************************************************

/* This is a list of all installed packages and their respective versions. We 
provide the exact ado files and their corresponding help files for ease.*/

if $ado_files == 1 {
  adopath ++ "$KP_ado/ado"
}

else if $install_packages == 1 { // Version Number        Version Date          
  ssc install codensity           // 1.0.0                 16Apr2025
  ssc install distinct            // 1.2.1                 01Mar2012
  ssc install distplot            // 2.2.2                 15Sep2017
  ssc install estout              // 3.17                  02Jun2014
  ssc install freqindex           // 1.3.1                 ..Apr2019
  ssc install ftools              // 2.49.1                08Aug2023
  ssc install gtools              // 1.10.1                05Dec2022
  ssc install ivreg2              // 4.1.12                14Aug2024
  ssc install ivreghdfe           // 1.1.3                 04Jan2023
  ssc install lassopack           // 1.4.3 (lasso2 1.0.13) 05Jan2024
  ssc install leebounds           // 1.5                   17Jul2013
  ssc install matchit             // 1.5.2                 20May2020
  ssc install pdslasso            // 1.0.03                04Sep2018
  ssc install randtreat           // 1.4                   05Apr2017
  ssc install ranktest            // 2.0.04                21Sep2020
  ssc install reghdfe             // 6.12.5                27Dec2023
  ssc install tabout              // 2.0.8                 15Mar2019
  ssc install unique              // 1.2.4                 17Jun2020
}


********************************************************************************
**#                             7. Run Scripts
********************************************************************************

*-------------------------------------------------------------------------------
**##                             7.1 Cleaning 
*-------------------------------------------------------------------------------

* 1) Clean and deidentify PMO data
if $clean_PMO == 1 {
  
  ** Merge
  if $merge_PMO == 1 {

    * Merge different data transfers together (long)
    do "$KP_do/1 Cleaning/PMO/Merge/PMO_merge_long_b1-22.do"
    
    * Merge different data transfers together (wide)
    do "$KP_do/1 Cleaning/PMO/Merge/PMO_merge_wide_b1-22.do"

    * Add extra vars from later transfers (wide)
    do "$KP_do/1 Cleaning/PMO/Merge/PMO_merge_wide_add_vars_b1-22.do"

    * Add baseline vars from later transfers (wide)
    do "$KP_do/1 Cleaning/PMO/Merge/PMO_merge_wide_add_vars_baseline_b1-22.do"

    * Add in location data (wide)
    do "$KP_do/1 Cleaning/PMO/Merge/PMO_merge_wide_add_vars_domicili_b1-22.do"

    * Merge all the wide data together and clean
    do "$KP_do/1 Cleaning/PMO/Merge/PMO_merge_wide_all.do"
  }

  ** Clean

  * creates cleaned long datasets for PMO data
  do "$KP_do/1 Cleaning/PMO/Clean/PMO_clean_long_b1-22.do"

}


* 2) Clean SAKERNAS data
if $clean_SAKERNAS == 1 {

  * clean Aug 2018 SAKERNAS
  do "$KP_do/1 Cleaning/Sakernas/Clean/clean_SAKERNAS_aug2018.do"

  * clean Feb 2019 SAKERNAS
  do "$KP_do/1 Cleaning/Sakernas/Clean/clean_SAKERNAS_feb2019.do"

  * clean Aug 2019 SAKERNAS
  do "$KP_do/1 Cleaning/Sakernas/Clean/clean_SAKERNAS_aug2019.do"

  * clean Feb 2020 SAKERNAS
  do "$KP_do/1 Cleaning/Sakernas/Clean/clean_SAKERNAS_feb2020.do"

  * clean Aug 2020 SAKERNAS
  do "$KP_do/1 Cleaning/Sakernas/Clean/clean_SAKERNAS_aug2020.do"

  * clean Feb 2021 SAKERNAS
  do "$KP_do/1 Cleaning/Sakernas/Clean/clean_SAKERNAS_feb2021.do"

  * clean Aug 2021 SAKERNAS
  do "$KP_do/1 Cleaning/Sakernas/Clean/clean_SAKERNAS_aug2021.do"

}

* 3) Clean SUSENAS data
if $clean_SUSENAS == 1 {

  * Convert .dbf files to .dta
  do "$KP_do/1 Cleaning/Susenas/Clean/load_dbfs.do"

  * clean Mar 2018 SUSENAS
  do "$KP_do/1 Cleaning/Susenas/Clean/clean_SUSENAS_mar2018.do"

  * clean Sep 2018 SUSENAS
  do "$KP_do/1 Cleaning/Susenas/Clean/clean_SUSENAS_sep2018.do"

  * clean Mar 2019 SUSENAS
  do "$KP_do/1 Cleaning/Susenas/Clean/clean_SUSENAS_mar2019.do"

  * clean Mar 2020 SUSENAS
  do "$KP_do/1 Cleaning/Susenas/Clean/clean_SUSENAS_mar2020.do"

  * clean Sep 2020 SUSENAS
  do "$KP_do/1 Cleaning/Susenas/Clean/clean_SUSENAS_sep2020.do"

  * clean Mar 2021 SUSENAS
  do "$KP_do/1 Cleaning/Susenas/Clean/clean_SUSENAS_mar2021.do"

  * clean Sep 2021 SUSENAS
  do "$KP_do/1 Cleaning/Susenas/Clean/clean_SUSENAS_sep2021.do"

  * clean Mar 2022 SUSENAS
  do "$KP_do/1 Cleaning/Susenas/Clean/clean_SUSENAS_mar2022.do"

  * Create March-Sep 2020 Panel, Industry x Java shock datasets
  do "$KP_do/1 Cleaning/Susenas/Clean/merge_marchsep20.do"

  * Create Java or not classification for each province
  do "$KP_do/1 Cleaning/Susenas/Clean/provid_anonymize.do"

}

* 4) Clean and deidentify survey 2021 data
if $clean_survey21 == 1 {

  * cleans and prepares survey data for analysis
  do "$KP_do/1 Cleaning/Survey/2021/Clean/clean_survey_21.do"

}

* 5) Clean and deidentify survey 2022 data
if $clean_survey22 == 1 {

  * cleans and prepares survey data for analysis
  do "$KP_do/1 Cleaning/Survey/2022/Clean/clean_survey_22.do"

  * combine with survey 2021
  do "$KP_do/1 Cleaning/Survey/2022/Clean/combine_control_group_21_22.do"

}

* 6) Clean Combined data
if $clean_combined == 1 {

  * cleans and prepares survey data for analysis
  do "$KP_do/1 Cleaning/Combined/demographics_match_clean.do"

}

*-------------------------------------------------------------------------------
**##                              7.2 Analysis
*-------------------------------------------------------------------------------

if $tables_figures == 1 {

  *--------------------------- Table 1, A7, A14 -------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/program_receipt_counts.do"

  *------------------------------ Table 2, A11a --------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/report_win_rates.do"

  *-------------------------------- Table 3 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/report_applywin_balance.do"

  *----------------------------- Table 4, A13 ---------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/targeting_table.do"

  *--------------------- Table 5, A11b, A11c, A16, A19-A24 ----------------------*
  do "$KP_do/2 Analysis/2 Impacts/sus_sak_outcomes_pwin_master.do"

  *------------------- Table 6, 7, A17, A18, A30, A31, A32 --------------------*
  do "$KP_do/2 Analysis/2 Impacts/onlinesurvey_outcomes_master.do"

  *----------------------------- Figures 1, 4 ---------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/reportwin_demogmatch_by_batchround.do"

  *----------------------------- Figures 2, 3 ---------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/win_getcash_diff_dist.do"

  *------------------------------- Table A1 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/lottery_inputs_reg.do"

  *------------------------------- Table A2 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/timeline_applywin.do"

  *------------------------------- Table A3 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/onlinesurvey_responserates.do"

  *----------------------------- Table A4, A6 ---------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/balance_pmo_s22.do"

  *------------------------------- Table A5 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/balance_sussak_pwin_det.do"

  *------------------------------- Table A8 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/reportapply_by_won.do"
  
  *------------------------------ Table A9 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/report_win_rates_surveytaker.do"

  *------------------------------ Table A10 ---------------------------------*
  do "$KP_do/2 Analysis/2 Impacts/sus_outcomes_pwin_master_surveytaker.do"

  *------------------------------ Table A12 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/demographics_match_bywave.do"

  *------------------------------ Table A15 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/report_win_rates_gc.do"

  *---------------------------- Table A25, A26 --------------------------------*
  do "$KP_do/2 Analysis/2 Impacts/leebounds_outcomes_master.do"

  *------------------------------ Table A27 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/pmovars_by_online22response.do"

  *------------------------------ Table A28 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/online_national_applyselect_compare.do"

  *------------------------------ Table A29 -----------------------------------*
  do "$KP_do/2 Analysis/1 Descriptive/course_descriptives.do"

}

// DONE
